// Copyright (C) 2005 Taylor L. Riché <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// 	$Id: Function.hh 411 2006-05-26 16:26:59Z richetl $	
//

#ifndef PPFUNCTION_HH
#define PPFUNCTION_HH

#include <list>
#include <map>
#include <set>
#include <string>
#include <iostream>

#ifdef LEE_BUILD
#include "Composable.hh"
#include "Container.hh"
#include "Event.hh"
#include "Packet.hh"
#include "ProcUnit.hh"
#include "Synchronized.hh"
#else
#include <lee/Composable.hh>
#include <lee/Container.hh>
#include <lee/Event.hh>
#include <lee/Packet.hh>
#include <lee/ProcUnit.hh>
#include <lee/Synchronized.hh>
#endif

namespace lee
{

  typedef void PktData;
  class Event;
  class Container;
  class ProcUnit;

  struct FuncExit
  {
    int exit;
    PktData * pkt;
    uint32_t pktSize;
  };

  class Function : public Composable, public Synchronized
  {
  protected:
    //std::list<FuncExit *> * exitList;
    ProcUnit * currProc;
    std::set<ProcUnit *>::const_iterator pIt;
    std::set<ProcUnit *> procSet;
    bool ready;
    bool recursive;
    uint32_t workval;
    pthread_key_t exitKey;
    pthread_key_t eventKey;
    void setRecursive(bool b) {recursive = b;}
  public:
    Function() : recursive(false), workval(1) {}
    Function(std::string s, std::string t) : Composable(s,t), currProc(NULL),
					     ready(false),recursive(false),
                                             workval(1) {
      procSet.clear();
      assert(procSet.empty());
    }
    virtual ~Function() {}
    virtual void processData(PktData * const pkt, uint32_t pktSize) = 0;
//    virtual std::pair<std::list<FuncExit *> *, std::list<Event *> * > 
//     processDataWrapper(PktData * const pkt, uint32_t pktSize) {
//       exitList = new std::list<FuncExit *>;
//       eventList = new std::list<Event *>;
//       std::cerr << name << " is processing" << std::endl;
//       this->processData(pkt, pktSize);
//       std::pair<std::list<FuncExit *> *, std::list<Event *> * > p;
//       p.first = exitList;
//       assert(!exitList->empty());
//       p.second = eventList;
//       return p;
//     }
    inline bool isRecursive() {return recursive;}
    inline bool isScheduled() {return ready;}
    inline bool isScheduled(ProcUnit * pu) {return(procSet.find(pu)!=procSet.end());}
    inline uint32_t getWork() {return workval;}
    inline void setWork(uint32_t wv) {workval = wv; return;}
    inline void setKeys(pthread_key_t exK, pthread_key_t evK) {exitKey = exK; eventKey = evK;return;}
    void drop(PktData * pkt) {
      FuncExit * ex = new FuncExit();
      ex->exit = 0;
      ex->pkt = NULL;
      ex->pktSize = 0;
      Packet * fullPkt = static_cast<Packet *>(pkt);
      delete fullPkt;
      std::list<lee::FuncExit *> * exitList = static_cast<std::list<lee::FuncExit *> * >(pthread_getspecific(exitKey));
      exitList->push_back(ex);
    }
    Subscription * getMasterSub() {return dynamic_cast<Subscription *>(masterCont);}
    void schedule(ProcUnit * p);
//     {
//       Synchronized::Scope mutex(this);
//       currProc = p;
//       std::cerr << "Set p" << std::endl;
//       notifyAll();
//     }
    ProcUnit * getProc();
//     {
//       Synchronized::Scope mutex(this);
//       while(currProc == NULL) {
// 	wait();
//       }
//       std::cerr << "Returning p" << std::endl;
//       return currProc;
//     }
  };

}
#endif // PPFUNCTION_H
